<?php
/**
*
* @package RCB - R/C Booking
* @version $Id: edit.php 8 2009-02-12 14:39:45Z scott $
* @copyright (c) 2008 Scott Deakin
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'rcb/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/rcb');

page_header($user->lang['RCB_TITLE']);

$template->set_filenames(array(
    'body' => 'rcb/edit_body.html',
));

if(!$auth->acl_get('m_edit'))
{
	trigger_error($user->lang['RCB_NOT_MOD_MEETING']); 
}

$meeting_id = request_var('meeting_id',0);
$action = request_var('action','edit');

function get_meeting_details($db, $template, $meeting_id)
{

	$sql = 'SELECT meeting_id, date, description, status
			FROM ' . RCB_MEETING_TABLE . '
			WHERE meeting_id = ' . $meeting_id;
	$result = $db->sql_query($sql);

	if($result == true && $row = $db->sql_fetchrow($result)) 
	{
		$template->assign_vars(array(
			'MEETING_ID' => $row['meeting_id'],
			'DATE' => $row['date'],
			'DESCRIPTION' => $row['description'],
			'STATUS' => $row['status'],
			'S_STATUS_OPEN' => ($row['status'] == 'Open' ? 'selected' : ''),
			'S_STATUS_CLOSED' => ($row['status'] == 'Closed' ? 'selected' : ''),
			'S_STATUS_CANCELLED' => ($row['status'] == 'Cancelled' ? 'selected' : '')
			 ));
	}
	else
	{
		trigger_error($user->lang['RCB_NO_MEETING']);
	}

	// get a list of classes booked in

	$sql = 'SELECT c.class_id, c.rct_class, c.max_size, c.description, s.meeting_id
			FROM rcb_class AS c
			LEFT JOIN 
			(
				SELECT class_id, meeting_id
				FROM rcb_meeting_class 
				WHERE meeting_id = ' . (int) $meeting_id . '
			) AS s
			ON s.class_id = c.class_id
			ORDER BY c.class_id';
	$result = $db->sql_query($sql);
	while($row = $db->sql_fetchrow($result)) {
		$template->assign_block_vars('classrow', array(
			'S_CHECKED' => ($row['meeting_id'] == $meeting_id ? 'checked' : ''),
			'CLASS_ID' => $row['class_id'],
			'RCT_CLASS' => $row['rct_class'],
			'MAX_SIZE' => $row['max_size'],
			'DESCRIPTION' => $row['description']
		));

	}
}



// now decide what to do
// first lets see if we are adding a new class or not
$submit = request_var('submit','');

switch($submit)
{
	case 'Add':
		$sql_array = array(
			'description'	=> request_var('new_description',''),
			'max_size'	=> request_var('new_max_size',10),
			'rct_class'	=> request_var('new_rct_class',0)
		);

		$sql = 'INSERT INTO ' . RCB_CLASS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_array);
		$result = $db->sql_query($sql);
		$action = 'edit';		
		break;
	case 'Submit':
		// keep the action as it it is....
		break;
	case 'Cancel':
		redirect('index.php');
		break;
	default :
		break;
}

switch($action)
{

	case 'delete':
		$sql = 'DELETE 
				FROM ' . RCB_MEETING_TABLE . '
				WHERE meeting_id = ' . (int) $meeting_id;
		$db->sql_query($sql);
		redirect('index.php');
		break;

	case 'new':
		// insert a new row into the meeting table
		// $sql = 'INSERT INTO ' . RCB_MEETING_TABLE .'
		$sql_array = array(
			'date'			=> request_var('date',''),
			'description'	=> request_var('description',''),
			'status'		=> 'Open'
		);
		$sql = 'INSERT INTO ' . RCB_MEETING_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_array);
		$result = $db->sql_query($sql);

        $meeting_id = $db->sql_nextid();
		$template->assign_vars(array('S_ACTION' => 'update',
									'MEETING_ID' => $meeting_id));
		get_meeting_details($db, $template, $meeting_id);
		break;

	case 'delete_class':
		// well, just remove it then!
		$sql = 'DELETE FROM '.RCB_CLASS_TABLE.' WHERE class_id = '. (int) request_var('class_id',0);
		$db->sql_query($sql);
		$template->assign_vars(array('S_ACTION' => 'update',
									'MEETING_ID' => $meeting_id));
		get_meeting_details($db, $template, $meeting_id);
		break;

	case 'edit':
		$template->assign_vars(array('S_ACTION' => 'update',
									'MEETING_ID' => $meeting_id));
		get_meeting_details($db, $template, $meeting_id);
		break;

	case 'update':

		// first delete all current meeting assignments
		$sql = 'DELETE FROM ' . RCB_MEETING_CLASS_TABLE . '
				WHERE meeting_id = ' . (int) $meeting_id;
		$db->sql_query($sql);

		$class_id_array = request_var('class_id',array('' => 0));
		$c_description_array = request_var('c_description',array('' => ''));
		$rct_class_array = request_var('rct_class',array('' => ''));
		$max_size_array = request_var('max_size',array('' => ''));
		$available_array = request_var('available',array('' => ''));

		// update the class details and meeting assignments
		for($i = 0; $i < sizeof($class_id_array); $i++)
		{
			$sql_array = array(
				'description'	=> $c_description_array[$i],
				'max_size'	=> $max_size_array[$i],
				'rct_class'	=> $rct_class_array[$i]
			);
			$sql = 'UPDATE ' . RCB_CLASS_TABLE . '
					SET ' . $db->sql_build_array('UPDATE', $sql_array) . ' 
					WHERE class_id = ' . (int) $class_id_array[$i];
			$result = $db->sql_query($sql);
			if ($available_array[$i] == 'on') {	
				$sql_array = array(
					'class_id'		=> $class_id_array[$i],
					'meeting_id'	=> $meeting_id
				);
				$sql = 'INSERT INTO ' . RCB_MEETING_CLASS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_array);
				$result = $db->sql_query($sql);
			}	
		}

		// update the meeting details and return to the meeting list

		$sql_array = array(
			'date'			=> request_var('date',''),
			'description'	=> request_var('description',''),
			'status'		=> request_var('status','')
		);

		$sql = 'UPDATE ' . RCB_MEETING_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $sql_array) . ' 
				WHERE meeting_id = ' . (int) $meeting_id;
		$result = $db->sql_query($sql);
		if($result == true) 
		{
			// redirect back to the meeting list
			redirect('index.php');
		} 
		else
		{
			trigger_error('Failed to update');
		}
		break;

	default:

		trigger_error("Don't know what to do!");

	break;
} 

page_footer();
?>
